<?php


namespace app\admin\controller;

use app\admin\model\SystemAdmin;
use app\middleware\VerifyJwt;
use Firebase\JWT\JWT;
use think\facade\Db;
use think\facade\Request;

class Index {

	//登录验证
	function check_login() {
		$input = input();

		if($input['type']=='') return json(['code' => 0, 'msg' => '参数错误']);

		if (empty($input['username'])) return json(['code' => 0, 'msg' => '用户名不能为空']);

		if (empty($input['password'])) return json(['code' => 0, 'msg' => '密码不能为空']);

		$username = trim($input['username']);

		$password = trim($input['password']);

		switch ($input['type']) {
				//管理登录
			case 1:

				$info = Db::name('system_admin')->where('username', $username)->find();

				break;
				//代理登录
			case 2:

				$info=Db::name('agent')->where('number', $username)->find();

				break;
		}

		if (empty($info)||$info=='') return json(['code' => 0, 'msg' => '用户不存在']);

		if ($info['password'] != md5($password)) return json(['code' => 0, 'msg' => '密码错误']);

		if($info['status']==0) return json(['code' => 0, 'msg' => '你已被禁用']);

		//登录成功
		$id = $info['id'];//用户id

		//生成token
		$key = "example_key";
		$user_data = array(
			"userId" => $id,
			'type'=>$input['type'],
			'exp' => time() + 60 * 60 * 24 * 3  //3天后过期
		);

		$token = JWT::encode($user_data, $key);

		$info['roles'] = ['admin'];

		switch ($input['type']) {
			//管理登录
			case 1:

				Db::name('system_admin')->where('id', $id)->update(['last_ip'=>\request()->ip(),'last_time'=>time()]);

				$info['name']=$info['username'];

				break;
			//代理登录
			case 2:

				Db::name('agent')->where('id', $id)->update(['last_ip'=>\request()->ip(),'last_time'=>time()]);

				$info['name']=$info['nick_name'];

				break;
		}

		return json(['code' => 1, 'msg' => '登录成功', 'data' => ['jwt' => $token, 'user_info' => $info]]);
	}

	//获取登录人的路由
	function get_AllroleData() {
		$headr=input();

		$userinfo=Pubilc::check_jwt($headr['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		if(empty($id))return json(['code' => 0,'msg'=>'用户参数错误']);

		if(empty($headr['type'])) return json(['code' => 0, 'msg' => 'type参数错误']);

		switch ($headr['type']) {
			case 1:

				$info=Db::name('system_admin')->where('id', $id)->find();

				break;
			case 2:

				$info=Db::name('agent')->where('id', $id)->find();

				break;
		}

		$arr=explode(',',$info['arr']);

		$o=[];

		for ($i1 = 0; $i1 < count($arr); $i1++) {

			$arr[$i1] = intval($arr[$i1]);

			$p = Db::name('system_menu')->where('id', $arr[$i1])->where('pid', 0)->find();

			if (!empty($p)) {

				array_push($o, $p);

				for ($i3 = 0; $i3 < count($o); $i3++) {

					$o[$i3]['meta']['title'] = $o[$i3]['title'];

					$o[$i3]['meta']['noCache'] = $o[$i3]['noCache'];

					$o[$i3]['meta']['icon'] = $o[$i3]['icon'];

					$o[$i3]['meta']['roles'] = ['admin'];

					$o[$i3]['id'] = intval($o[$i3]['id']);

					$c = Db::name('system_menu')->where('pid', $o[$i3]['id'])->where('id', 'in', $arr)->select()->all();

					if (!empty($c)) {

						for ($i8 = 0; $i8 < count($c); $i8++) {

							$c[$i8]['meta']['title'] = $c[$i8]['title'];

							$c[$i8]['meta']['icon'] = $c[$i8]['title'];

							$c[$i8]['meta']['roles'] = ['admin'];

							$c[$i8]['meta']['noCache'] = $c[$i8]['noCache'];
						}
						$o[$i3]['children'] = $c;
					} else {
						$o[$i3]['children'] = [];;
					}
				}
				$data = $o;
			}
		}

		return json(['code' => 1, 'data' =>$data ]);
	}

	//获取后台管理员所有人列表
	function get_rules() {
		$data = Db::name('system_admin')->select()->all();

		for ($i = 0; $i < count($data); $i++) {

			$o = [];

			$arr = explode(',', $data[$i]['arr']);

			for ($i1 = 0; $i1 < count($arr); $i1++) {

				$arr[$i1] = intval($arr[$i1]);

				$p = Db::name('system_menu')->where('id', $arr[$i1])->where('pid', 0)->find();

				if (!empty($p)) {

					array_push($o, $p);

					for ($i3 = 0; $i3 < count($o); $i3++) {

						$o[$i3]['meta']['title'] = $o[$i3]['title'];

						$o[$i3]['meta']['noCache'] = $o[$i3]['noCache'];

						$o[$i3]['meta']['icon'] = $o[$i3]['icon'];

						$o[$i3]['meta']['roles'] = ['admin'];

						$o[$i3]['id'] = intval($o[$i3]['id']);

						$c = Db::name('system_menu')->where('pid', $o[$i3]['id'])->where('id', 'in', $arr)->select()->all();

						if (!empty($c)) {

							for ($i8 = 0; $i8 < count($c); $i8++) {

								$c[$i8]['meta']['title'] = $c[$i8]['title'];

								$c[$i8]['meta']['icon'] = $c[$i8]['title'];

								$c[$i8]['meta']['roles'] = ['admin'];

								$c[$i8]['meta']['noCache'] = $c[$i8]['noCache'];
							}
							$o[$i3]['children'] = $c;
						} else {
							$o[$i3]['children'] = [];;
						}
					}
					$data[$i]['routes'] = $o;
				}
			}
		}
		return json(['code' => 1, 'data' => $data]);
	}

	//添加管理员
	function create_acctount() {
		$input = input();


		if (empty($input['username'])) return json(['code' => 0, 'msg' => '请输入用户名']);

		if (empty($input['password'])) return json(['code' => 0, 'msg' => '请输入密码']);

		if (empty($input['name'])) return json(['code' => 0, 'msg' => '请输入真实姓名']);

		if (empty($input['position'])) return json(['code' => 0, 'msg' => '请输入职务']);

		if (empty($input['routes'])) return json(['code' => 0, 'msg' => '请选择权限']);

		$p=[];
		$k=[];
		for ($i=0;$i<count($input['routes']);$i++){

			array_push($p,$input['routes'][$i]['id']);

			for ($i2=0;$i2<count($input['routes'][$i]['children']);$i2++){

				array_push($k,$input['routes'][$i]['children'][$i2]['id']);
			}
		}
		$data=array_merge($p,$k);
		$arr=join(',',$data);

		$account = Db::table('system_admin')->where('username', $input['username'])->find();

		if (!empty($account)) return json(['code' => 0, 'msg' => '用户名已被注册']);

		$password = md5($input['password']);

		$data = [
			'username' => $input['username'],
			'password' => $password,
			'name' => $input['name'],
			'addtime' => time(),
			'last_time' => 0,
			'position' => $input['position'],
			'tel' => $input['tel'],
			'roles' => 'admin',
			'introduction' => '',
			'arr' => $arr,
			'status' => 1,
			'avatar' => 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3280696440,2906944118&fm=26&gp=0.jpg',
		];

		$result = Db::name('system_admin')->insert($data);

		if (!$result) {
			return json(['code' => 0, 'msg' => '创建失败']);
		}
		return json(['code' => 1, 'msg' => '创建成功']);
	}

	//删除管理员
	function del_account() {
		$id = input('id');

		$account = Db::table('system_admin')->where('id', $id)->find();

		if (empty($account)) return json(['code' => 0, 'msg' => '用户不存在']);

		$result = Db::table('system_admin')->delete($id);

		if (!$result) return json(['code' => 0, 'msg' => '删除失败']);

		return json(['code' => 1, 'msg' => '删除成功']);
	}

	function show_err() {
		return json(['code' => 0, 'msg' => input('msg')]);
	}

	//编辑管理员
	function edit_admin() {
		$input = input();

		if(empty($input['id'])) return json(['code' => 0, 'msg' => '参数错误']);

		$account = Db::table('system_admin')->where('id', $input['id'])->find();

		if (empty($account)) return json(['code' => 0, 'msg' => '用户不存在']);

		if (empty($input['username'])) return json(['code' => 0, 'msg' => '请输入用户名']);

		if (empty($input['password'])) return json(['code' => 0, 'msg' => '请输入密码']);

		if (empty($input['name'])) return json(['code' => 0, 'msg' => '请输入真实姓名']);

		if (empty($input['position'])) return json(['code' => 0, 'msg' => '请输入职务']);

		if (empty($input['routes'])) return json(['code' => 0, 'msg' => '请选择权限']);

		$p=[];
		$k=[];
		for ($i=0;$i<count($input['routes']);$i++){

			array_push($p,$input['routes'][$i]['id']);

			for ($i2=0;$i2<count($input['routes'][$i]['children']);$i2++){

				array_push($k,$input['routes'][$i]['children'][$i2]['id']);
			}
		}
		$data=array_merge($p,$k);

		$arr=join(',',$data);

		$admininfo=Db::table('system_admin')->where('id', $input['id'])->find();

		//没改密码
		if ($input['password'] == $admininfo['password']) {

			$pwd = $admininfo['password'];

		} else {
			$pwd = md5($input['password'] );
		}

		$data1 = [
			'username' => $input['username'],
			'password' => $pwd,
			'name' => $input['name'],
			'addtime' => time(),
			'last_time' => 0,
			'position' => $input['position'],
			'tel' => $input['tel'],
			'roles' => 'admin',
			'introduction' => '',
			'arr' => $arr,
		];

		$result = Db::name('system_admin')->where('id', $input['id'])->update($data1);

		if (!$result) {
			return json(['code' => 0, 'msg' => '编辑失败']);
		}
		return json(['code' => 1, 'msg' => '编辑成功']);
	}

	//编辑管理员状态
	function edit_state(){
		$input = input();

		$account = Db::table('system_admin')->where('id', $input['id'])->find();

		if (empty($account)) return json(['code' => 0, 'msg' => '用户不存在']);

		$result = Db::name('system_admin')->where('id', $input['id'])->update(['status'=>$input['status']]);

		if (!$result) return json(['code' => 0, 'msg' => '编辑失败']);

		return json(['code' => 1, 'msg' => '编辑成功']);
	}




}